﻿var isEnglish = false;
var arrindlang = new Array();
var arrind = new Array();

arrindlang[0] = "行业选择器";
arrindlang[1] = "关闭";
arrindlang[2] = "取消 ";
arrindlang[3] = "对不起！最多只能选择1项！";
arrindlang[4] = "已选项：";
arrindlang[5] = "请选择行业：";

function IndustrySelecter(selecterName) {
    this.selecterName = selecterName;
    this.divIndustryData = null;
    this.objIndustryName = null;
    this.objIndustryID = null;

    this.init = function(objIndustryName, objIndustryID) {
        this.objIndustryName = objIndustryName;
        this.objIndustryID = objIndustryID;
        this.objIndustryName.value = this.getSelectedIndustryName();
    }

    this.getStyle = function() {
        var str = '';
        str += '<style type="text/css">';
        str += 'a.industry, a.selindustry{ font-size:12px;float:left;padding:4px 5px 4px 20px;width:126px;height:14px;color:#000;text-decoration:none; }';
        str += 'a.selindustry:hover{ background-color:#FFCC00; }';
        str += 'a.industry{ padding:1px 5px 1px 5px; width:195px;height:20px; cursor:default; }';
        str += 'a.selindustry{ background:url(/content/images/del.gif) no-repeat 5px center; padding:3px 0px 0px 22px; width:88px; }';
        str += 'a.selindustry:hover{ background:#FFCC00 url(/content/images/delhover.gif) no-repeat 5px center; }';
        str += '</style>';
        return str;
    }

    this.getDivBack = function() {
        var str = '<div id="divIndustryBack" style="filter:Alpha(Opacity=40);opacity:.4;background:#fff;z-index:999;position:absolute;left:0px;top:0px;"></div>';
        return str;
    }
    
    this.getIframe = function(){
        var str = '<iframe id="ifrIndustryMain" style="filter:Alpha(Opacity=0);opacity:0;position:absolute;width:650px;height:430px;display:none;"></iframe>';
        return str;
    }

    this.getIndustryData = function() {
        var str = '';
        str += '        <div id="divIndustrySelected" style="padding:5px;height:22px;"><span style="float:left;font-size:12px; font-weight:bold; line-height:20px">' + arrindlang[4] + '</span></div>'; //已选项
        str += '        <div style="font-size:12px; background-color:#E9F8FE;border-top:1px dashed #006699;font-weight:bold;padding:5px; clear:both;color:#006699;">' + arrindlang[5] + '</div>';
        str += '        <div id="divIndustry">' + this.getIndustry() + '</div>'; //行业
        this.divIndustryData = str;
        return str;
    }

    this.getDivIndustryMain = function() {
        var str = '';
        str += this.getStyle();
        str += this.getDivBack();
        str += this.getIframe();
        str += '<div id="divIndustryMain" style="position:absolute;width:650px; display:none;">';
        str += '<table cellpadding="0" cellspacing="0" style="width:100%;cursor:move;" id="tbIndustryTitle">';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_t_l.gif);width:5px;height:30px;"></td>';
        str += '        <td style="background:url(/content/images/dia_t_c.gif);"><div style="float:left;color:#ffffff;font-weight:bold;font-size:14px;padding:5px;">' + arrindlang[0] + '</div><div style="float:right;"><a title="' + arrindlang[1] + '" id="btnClose" href="javascript:void(0);"><img onmousedown="' + this.selecterName + '.closeDiv()" src="/content/images/dia_close.gif" style="border:0px;" /></a></div></td>';
        str += '        <td style="background:url(/content/images/dia_t_r.gif);width:5px;height:30px;"></td>';
        str += '    </tr>';
        str += '</table>';
        str += '<table cellpadding="0" cellspacing="0" style="width:100%;">';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_m_l.gif);width:5px;"></td>';
        str += '        <td style="height:400px; background-color:#ffffff;" valign="top">';
        str += '        <div style="padding:8px 4px 5px 1px;" id="divIndustryData">';
        str += this.getIndustryData();
        str += '        </div>';
        str += '        </td>';
        str += '        <td style="background:url(/content/images/dia_m_r.gif);width:6px;"></td>';
        str += '    </tr>';
        str += '    <tr>';
        str += '        <td style="background:url(/content/images/dia_b_l.gif);width:5px;height:6px;"></td>';
        str += '        <td style="background:url(/content/images/dia_b_c.gif);height:6px;"></td>';
        str += '        <td style="background:url(/content/images/dia_b_r.gif);width:6px;height:6px;"></td>';
        str += '    </tr>';
        str += '</table>';
        str += '</div>';
        return str;
    }

    this.getIndustry = function() {
        var str = '';
        str += '<table border="0" width="100%" cellpadding="0" cellspacing="5">';
        str += '    <tr>';
        str += '        <td>'
        for (var i = 0; i < arrind.length; i++) {
            str += '        <a title="' + arrind[i].classname + '" id="linkIndustry' + arrind[i].classid + '" class="industry" href="javascript:void(0)"><input type="checkbox" id="chIndustry' + arrind[i].classid + '" value="' + arrind[i].classid + '" onclick="return ' + this.selecterName + '.selectIndustry(this, \'' + arrind[i].classname + '\')" /><label for="chIndustry' + arrind[i].classid + '">' + this.trimString(arrind[i].classname, 12, 25) + '</label></a>';
        }
        str += '        </td>';
        str += '    </tr>';
        str += '</table>';
        return str;
    }

    this.trimString = function(str, cnLen, enLen) {
        var maxLen = isEnglish ? enLen : cnLen;
        if (str.length > maxLen)
            str = str.substr(0, maxLen) + "...";
        return str;
    }

    this.getIndustryName = function(industryid) {
        var i;
        for (i = 0; i < arrind.length; i++) {
            if (arrind[i].classid == industryid)
                return arrind[i].classname;
        }
        return "";
    }

    this.getSelectedIndustryName = function() {
        var idstr = this.objIndustryID.value;
        if (idstr == "") return "";
        if (idstr.indexOf(",") == -1) return this.getIndustryName(idstr);
        var arrid = idstr.split(",");
        var rvalue = "";
        for (var i = 0; i < arrid.length; i++) {
            var industryName = this.getIndustryName(arrid[i]);
            if (rvalue == "") rvalue = industryName;
            else if (industryName != "") { rvalue += "+" + industryName; }
        }
        return rvalue;
    }

    this.selectIndustry = function(sender, industryName) {
        var chIndustry = document.getElementById("chIndustry" + sender.value);

        if (sender.checked) {
            var val = this.objIndustryID.value.split(",");
            if (this.objJobID.value != "") {
                if (val.length >= 1) {
                    alert(arrindlang[3]);
                    return false;
                }
            }
            if (this.objIndustryID.value == "") this.objIndustryID.value = sender.value;
            else this.objIndustryID.value += ',' + sender.value;
            this.objIndustryName.value = this.getSelectedIndustryName();
            this.addIndustry(sender.value, industryName);

            if (chIndustry && sender != chIndustry) chIndustry.checked = true;
        }
        else {
            if (chIndustry && sender != chIndustry) chIndustry.checked = false;
            this.delIndustry(sender.value);
        }
    }

    this.addIndustry = function(industryID, industryName) {
        var linkSelIndustry = getElem("linkSelIndustry" + industryID, "a", document.getElementById("divIndustrySelected"));
        linkSelIndustry.className = "selindustry";
        linkSelIndustry.innerHTML = this.trimString(industryName, 5, 10);
        linkSelIndustry.href = "javascript:" + this.selecterName + ".delSelectedIndustry(" + industryID + ")";
        linkSelIndustry.title = arrindlang[2] + industryName;
    }

    this.delSelectedIndustry = function(industryID) {
        this.delIndustry(industryID);

        var chIndustry = document.getElementById("chIndustry" + industryID);
        if (chIndustry) chIndustry.checked = false;
    }

    this.delIndustry = function(industryID) {
        var val = this.objIndustryID.value.split(",");
        var industryvalue = "";
        for (var i = 0; i < val.length; i++) {
            if (val[i] != industryID) {
                if (industryvalue == "") industryvalue = val[i];
                else industryvalue += ',' + val[i];
            }
        }
        this.objIndustryID.value = industryvalue;
        this.objIndustryName.value = this.getSelectedIndustryName();

        var linkSelIndustry = document.getElementById("linkSelIndustry" + industryID);
        var divIndustrySelected = document.getElementById("divIndustrySelected");
        if (linkSelIndustry) divIndustrySelected.removeChild(linkSelIndustry);
    }

    this.closeDiv = function() {
        var divIndustryBack = document.getElementById("divIndustryBack");
        divIndustryBack.style.display = "none";

        var divIndustryMain = document.getElementById("divIndustryMain");
        divIndustryMain.style.display = "none";
        
        var ifrIndustryMain = document.getElementById("ifrIndustryMain");
        ifrIndustryMain.style.display = "none";
    }

    this.setDefaultIndustry = function() {
        var idstr = this.objIndustryID.value;
        if (idstr == "") return;

        var arrid = idstr.split(",");
        for (var i = 0; i < arrid.length; i++) {
            // 在选择器中显示默认选中行业
            this.addIndustry(arrid[i], this.getIndustryName(arrid[i]));

            // 把默认选中的行业打上勾
            var chIndustry = document.getElementById("chIndustry" + arrid[i]);
            if (chIndustry) chIndustry.checked = true;
        }
    }

    this.show = function(objIndustryName, objIndustryID) {
        var divIndustryBack = document.getElementById("divIndustryBack");
        divIndustryBack.style.width = document.documentElement.clientWidth + "px";
        divIndustryBack.style.height = document.documentElement.scrollHeight + "px";
        if (getInt(divIndustryBack.style.height) < 768) divIndustryBack.style.height = "768px";
        divIndustryBack.style.display = "";

        var divIndustryMain = document.getElementById("divIndustryMain");
        divIndustryMain.style.left = ((document.documentElement.clientWidth - getInt(divIndustryMain.style.width)) / 2) + "px";
        divIndustryMain.style.top = ((screen.availHeight - 500) / 2 + document.documentElement.scrollTop - 100) + "px";
        divIndustryMain.style.zIndex = 10000;
        divIndustryMain.style.display = "";
        
        var ifrIndustryMain = document.getElementById("ifrIndustryMain");
        ifrIndustryMain.style.left = divIndustryMain.style.left;
        ifrIndustryMain.style.top = divIndustryMain.style.top;
        ifrIndustryMain.style.zIndex = divIndustryMain.style.zIndex - 1;
        ifrIndustryMain.style.display = "";

        var tbIndustryTitle = document.getElementById("tbIndustryTitle");
        tbIndustryTitle.onmousedown = function(evt){
            curMoveObj = divIndustryMain;
            curMoveIframe = ifrIndustryMain;
            if (evt) { //firefox
                pX = evt.pageX - getInt(curMoveObj.style.left);
                pY = evt.pageY - getInt(curMoveObj.style.top);
            }
            else { //ie
                pX = event.x - curMoveObj.style.pixelLeft;
                pY = event.y - curMoveObj.style.pixelTop;
            }
        }

        // 复原数据区
        var divIndustryData = document.getElementById("divIndustryData");
        divIndustryData.innerHTML = this.divIndustryData;

        // 初始化控件
        this.init(objIndustryName, objIndustryID);

        // 显示默认选中行业
        this.setDefaultIndustry();
    }

    this.toString = function() {
        return this.getDivIndustryMain();
    }
}

var indSelecter;
$(function() {
$.getScript("/ajax/GetVocationList.aspx", function(data) {
        eval(data);
        indSelecter = new IndustrySelecter("indSelecter");
        $("body").append(indSelecter.toString());
    });
});